package com.dcits.account.service.impl;

import com.dcits.account.constants.BaseContants;
import com.dcits.account.entity.cto.InputObject;
import com.dcits.account.entity.cto.OutputObject;
import com.dcits.account.entity.po.*;
import com.dcits.account.mapper.AccountEntityMapper;
import com.dcits.account.mapper.BillEntityMapper;
import com.dcits.account.mapper.UserEntityMapper;
import com.dcits.account.service.interfaces.IBillService;
import com.dcits.account.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Auther: 2843896844@qq.com
 * @Date: 2021/06/11/19:50
 * @Description:
 */
@Service
public class BillServiceImpl implements IBillService {

    private Logger logger = LoggerFactory.getLogger(BillServiceImpl.class);

    @Autowired
    private BillEntityMapper billEntityMapper;

    @Autowired
    private UserEntityMapper userEntityMapper;

    @Autowired
    private AccountEntityMapper accountEntityMapper;

    @Override
    public void getBillListByPage(InputObject inputObject, OutputObject outputObject) {
        Map<String, Object> params = inputObject.getParams();
        Integer pageNum = MapUtils.getInteger(params, "pageNum");
        Integer pageSize = MapUtils.getInteger(params, "pageSize");
        String keyword = MapUtils.getString(params, "keyword");
        BillEntityExample billEntityExample = new BillEntityExample();
        if (StringUtils.isNotBlank(keyword)) {
            BillEntityExample.Criteria criteria = billEntityExample.createCriteria();
            criteria.andReasonLike("%" + keyword + "%");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<BillEntity> billEntities = billEntityMapper.selectByExample(billEntityExample);
        PageInfo<BillEntity> pageInfo = new PageInfo<>(billEntities);
        List<BillEntity> list = pageInfo.getList();
        Long total = pageInfo.getTotal();
        outputObject.setRtnCode(OutputObject.SUCCESS_CODE);
        outputObject.getBeans().addAll(list);
        outputObject.getBean().put("total", total);
    }

    @Override
    public void getLineData(InputObject inputObject, OutputObject outputObject) {
        Map<String, Object> params = inputObject.getParams();
        UserEntity user = (UserEntity) MapUtils.getObject(params, "user");
        Integer userId = user.getId();
        AccountEntityExample accountEntityExample = new AccountEntityExample();
        accountEntityExample.createCriteria().andUserIdEqualTo(userId);
        List<AccountEntity> accountEntities = accountEntityMapper.selectByExample(accountEntityExample);
        if(CollectionUtils.isEmpty(accountEntities)){
            outputObject.setRtnCode(OutputObject.FAILED_CODE);
            outputObject.setRtnMsg("系统逻辑错误，该用户无账户");
            return;
        }

        AccountEntity accountEntity = accountEntities.get(0);

        List<BigDecimal> dataIn = new ArrayList<>();
        List<BigDecimal> dataOut = new ArrayList<>();


        List<Map<String, Object>> last7Date = this.getLast7Date();
        for (Map<String, Object> map : last7Date) {
            int sizeIn = dataIn.size();
            int sizeOut = dataOut.size();
            map.put("accountId", accountEntity.getId());
            List<Map<String, Object>> maps = billEntityMapper.selectTypeAndCountByDate(map);
            if (CollectionUtils.isNotEmpty(maps)) {
                for (Map<String, Object> objectMap : maps) {
                    String type = MapUtils.getString(objectMap, "type");
                    if (type.equals(BaseContants.BILL_TYPE_IN)) {
                        dataIn.add((BigDecimal) objectMap.get("count"));
                    }
                    if (type.equals(BaseContants.BILL_TYPE_OUT)) {
                        dataOut.add((BigDecimal) objectMap.get("count"));
                    }
                }
            }
            if ((sizeIn + 1) != dataIn.size()) {
                dataIn.add(new BigDecimal(0));
            }
            if ((sizeOut + 1) != dataOut.size()) {
                dataOut.add(new BigDecimal(0));
            }
        }

        outputObject.setRtnCode(OutputObject.SUCCESS_CODE);
        outputObject.getBean().put("dataIn", dataIn);
        outputObject.getBean().put("dataOut", dataOut);
        outputObject.getBean().put("dateList", this.getlast7DateStr());
        outputObject.setRtnMsg("success");
    }

    /**
     * var legenddata = ['收入', '支出'];
     * var xAxisdata = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'];
     * var seriesdata = [
     * {
     * name: '收入',
     * type: 'line',
     * stack: '总量',
     * data: [120, 132, 101, 134, 90, 230, 210]
     * },
     * {
     * name: '支出',
     * type: 'line',
     * stack: '总量',
     * data: [220, 182, 191, 234, 290, 330, 310]
     * }
     *
     * @return
     */

    private List<Map<String, Object>> getLast7Date() {
        List<Map<String, Object>> maps = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            Date date = DateUtil.getDate(-i);
            String dateStr = new SimpleDateFormat("yyyy-MM-dd ").format(date);
            String beginTime = dateStr + "00:00:01";
            String endTime = dateStr + "23:59:59";
            Map<String, Object> map = new HashMap<>();
            map.put("beginTime", beginTime);
            map.put("endTime", endTime);
            maps.add(map);
        }
        return maps;
    }

    private List<String> getlast7DateStr() {
        List<String> maps = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            Date date = DateUtil.getDate(-i);
            String dateStr = new SimpleDateFormat("yyyy-MM-dd ").format(date);
            maps.add(dateStr);
        }
        return maps;
    }
}
